package com.tigerlee.stopmusic;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.AlarmManager;
import android.content.Context;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class AbstractActivity extends Activity{
	
	public Alarm mAlarm;
	public AlarmManager mAlarmManager;
	
	public AdView mAdView;
	public MyPreference mPreference;

	public boolean mIsSetTime = false;
	public boolean mIsRunning = false;
	
	
	private final String ADMOB_AD_UNIT_ID = "a150aa5821a6c0c";
	private static final String TAG = "AbstractActivity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mAlarm = new Alarm(System.currentTimeMillis());
		mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
		
		// Get preference Check alarm is set or not
		mPreference = new MyPreference(getApplicationContext());
		mIsRunning  = mPreference.isRunning();
		mIsSetTime  = mPreference.getTimerState();
	}
	/*
	 * This method will be called by two activities
	 * You MUST call this after calling setContentView
	 */
	
	public void setAdView() {
		// Create the adView
		mAdView = new AdView(this, AdSize.SMART_BANNER, ADMOB_AD_UNIT_ID);
		LinearLayout adLayout = (LinearLayout) findViewById(R.id.ad_layout);
		adLayout.removeAllViews();
		// Add the adView to it
		adLayout.addView(mAdView);
		AdRequest mAdRequest = new AdRequest();
		//mAdRequest.addTestDevice("1198B6A592AB4FD6F60FF978EC25AC27");
		// Initiate a generic request to load it with an ad
		mAdView.loadAd(mAdRequest);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mAdView != null) mAdView.destroy();		
	}
	/*
	 * This method will be called by two activities
	 * You MUST call this after calling setContentView
	 */
	public void setTimePanelView(boolean isSetTime) {
		Log.e(TAG, "Timer type : " + isSetTime);
		if (isSetTime) {// (TimeView) set stopping music by duration
			// 1. Set Current Time
			TextView textView = (TextView) findViewById(R.id.text_time_remain);
			// Calculate and then set current time
			String[] results = mAlarm.getRemainTimeToString(mAlarm.getAlarmTimeMillis());
			String hours = getString(R.string.hours, results[0]);
			String mins = getString(R.string.minutes, results[1]);
			String duration  = hours + mins;
			Log.e(TAG, duration + duration.length());

			// Change String to Spannable and then resizing
			final SpannableStringBuilder builder = new SpannableStringBuilder(
					duration);
			builder.setSpan(new AbsoluteSizeSpan(100), 0, results[0].length(),
					Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			
			builder.setSpan(new AbsoluteSizeSpan(23), results[0].length(),
					hours.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			
			builder.setSpan(new AbsoluteSizeSpan(100), hours.length(),
					hours.length() + results[1].length(),
					Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			
			builder.setSpan(new AbsoluteSizeSpan(23), hours.length()
					+ results[1].length(), hours.length() + mins.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			
			textView.setText(builder);
		} else {// (Duration) set stopping music by time
			TextView textView = (TextView) findViewById(R.id.text_time_remain);
			// Calculate and then set current time
			String time = String.format("%02d", mAlarm.getAlarmHour()) + ":"
					+ String.format("%02d", mAlarm.getAlarmMinute())
					+ mAlarm.getAMPM();

			// Change String to Spannable and then resizing
			final SpannableStringBuilder builder = new SpannableStringBuilder(
					time);
			builder.setSpan(new AbsoluteSizeSpan(110), 0, 5,
					Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
			builder.setSpan(new AbsoluteSizeSpan(40), 6, 8,
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			textView.setText(builder);
		}
	}

}
